var app = getApp()
Component({
    properties: {

    },
    data: {
      ischeck: true
    },
    methods: {
        clickcheck () {
          this.setData({
            ischeck: !this.data.ischeck
          })
        },
      getUserInfo (e) {
          var that = this
          console.log(e)
        if (e.detail.errMsg !='getUserInfo:ok') {
          wx.showToast({
            title: '请允许授权,否则无法正常使用小程序',
            icon: 'none'
          })
          return
          }else{
            if (that.data.ischeck) {
              wx.login({
                success: function (res) {
                  if (res.code) {
                    console.log(res.code)

                
                    that.getwxToken(e,res.code)
                  }
                }
              })
            } else {
              wx.showToast({
                title: '请先同意用户协议',
                icon: 'none'
              })
            }
          }
        },
        getwxToken (e,code) {
          var that = this;
          var params = {
            code: code,
            isSdx: 1
          }
          wx.request({
            url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/user/checkAuth',
            data: params,
            method: 'POST',
            header: {
              "content-type": "application/x-www-form-urlencoded; charset=UTF-8"
            },
            success: (res) => {
              console.log(res)
              if (res.data.success) {
                wx.setStorageSync('sessionKey', res.data.data.session_key)
                that.getOpenidLogin(e, res.data.data.openid, code, res.data.data.session_key, res.data.data.unionid)
              }else{
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
            }
          })
          // app.request('csq/user/checkAuth',params).then(res => {
          //   console.log(res)
          // })
        },
      getOpenidLogin(e, openid, code, key, unionid) {
        var that = this
        var params = {
          unionId: unionid,
          openId: openid,
          isSdx: 1,
          uuid: this.getUUid()
        }
        wx.request({
          url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/user/login/unionId',
          data: params,
          method: 'POST',
          header: {
            "content-type": "application/x-www-form-urlencoded; charset=UTF-8"
          },
          success: (res) => {
            console.log(res)
            if (res.data.code == 200) {
              wx.showToast({
                title: '登录成功',
                icon: 'none'
              })
              wx.removeStorageSync('userId')
              wx.removeStorageSync('token')
              wx.removeStorageSync('uuid')
              wx.setStorageSync('code',code)


              wx.setStorageSync('openid', openid)
              wx.setStorageSync('key', key)
              wx.setStorageSync('userId',res.data.data.user.id)
              wx.setStorageSync('token', res.data.data.token)
              wx.setStorageSync('uuid', params.uuid)
              wx.setStorageSync('avatarUrl', e.detail.userInfo.avatarUrl)
              wx.setStorageSync('nickName', e.detail.userInfo.nickName)
              that.getchangeUrl(e.detail.userInfo.avatarUrl,e.detail.userInfo.nickName)
              var pages = getCurrentPages() //获取加载的页面
              var currentPage = pages[pages.length - 1] //获取当前页面
              currentPage.loginCode()
            }else{
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          }
        })
      },
      getUUid() {
        var s = [];
        var hexDigits = "0123456789abcdef";
        for (var i = 0; i < 36; i++) {
          s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23] = "-";

        var uuid = s.join("");
        return uuid;
      },
      getchangeUrl (url,name) {
        var params = {
          name: name,
          userHeadPortraitPath: url,
          isWechatAuth: true
        }
        wx.request({
          url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/user/modify/auth',
          data: params,
          method: 'POST',
          header: {
            "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
            'token': wx.getStorageSync('token'),
            'uid': wx.getStorageSync('uuid')
          },
          success: (res) => {
            if (res.data.success) {
              return
            }else{
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          }
        })
      },
      goxieyi () {
        wx.navigateTo({
          url: '/package/index/pages/agreeXiexi/agreeXiexi',
        })
      },
      guangguang () {
        var pages = getCurrentPages() //获取加载的页面
        var currentPage = pages[pages.length - 1] //获取当前页面
        currentPage.setData({
          istokenPop: false
        })
      }
    }
})
